package com.example.a1.fannong.core.app;

import android.os.Environment;

import java.io.File;

/**
 * SD卡路径
 */
public class PathConfig {

    /**
     * 获取软件在SDK的保存路径
     *
     * @return 程序在SD卡的根目录
     */
    public static File getHomeFile() {
        File externalStorageDirectory = Environment.getExternalStorageDirectory();
        return new File(externalStorageDirectory, "SmartConsumer");
    }

    /**
     * 获取缓存路径
     *
     * @return 缓存路径
     */
    public static File getCacheFile() {
        return MyApplication.getInstance().getCacheDir();
    }

    /**
     * 获取零食目录，程序启动会被清空
     */
    public static File getTempFile() {
        return new File(getHomeFile(), "temp");
    }


    public static void deleteFile(File file) {
        if (file.isFile()) {
            file.deleteOnExit();
        } else {
            File[] files = file.listFiles();
            if (files != null) {
                for (File child : files) {
                    deleteFile(child);
                }
            }
            file.deleteOnExit();
        }
    }

    /**
     * 获取图片保存路径
     *
     * @return 图片存储文件夹
     */
    public static File getImageCacheHomeFile() {
        return new File(getHomeFile(), "image");
    }

    /**
     * 通用图片存储路径
     */
    public static File createImageCacheFile(String url) {
        String fileName = (url.hashCode() + "").replace("-", "_");
        return new File(getImageCacheHomeFile(), fileName);
    }

    public static String createImageCachePath(String url) {
        return createImageCacheFile(url).getAbsolutePath();
    }
}
